package com.shuzijun.leetcode.editor.cn;
public class MergeTwoSortedLists{
    public static void main(String[] args) {
         Solution solution = new MergeTwoSortedLists().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        ListNode p1 = l1;
        ListNode p2 = l2;
        ListNode head = new ListNode(0);
        ListNode tail = head;
        while (p1 != null && p2 != null){
            if (p1.val <= p2.val){
                tail.next = p1;
                tail = p1;
                p1 = p1.next;
            }else {
                tail.next = p2;
                tail = p2;
                p2 = p2.next;
            }
        }

        if (p1 != null) tail.next = p1;

        if (p2 != null) tail.next = p2;

        return head.next;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}